package com.msg.xml;

import com.msg.utils.ProjectConstants;

/*
 * This class is a controller which contains the method for creating database handler and XML handler
 * also provide methods for reading specific contents from XML and update the database
 */
public class XMLToDB {
	
	private String fileName = null;
	
	public XMLToDB(String fileName) {
		this.fileName = fileName;
	}
	/**
	 * create the instances of database handler and XML handler
	 */
	
	public void OpenHandlers() {
		XMLHandler.createInstance(fileName);
		DBHandler.createInstance(ProjectConstants.userName,
				                 ProjectConstants.password,
				                 ProjectConstants.DatabaseUrl);
	}
	
	/**
	 * close the handlers
	 */
	public static void closeHandlers() {
		XMLHandler.closeHandler();
		DBHandler.closeDB();
	}
	
	/**
	 * read the data of event table and opponent table from XML
	 * then update the database with these data
	 */
	public static void processEvent_Opponents(Property prop) {
		Table_Manipulator mani = new Eve_Opp_Manipulator(prop);
		mani.readXML();
		mani.writeDB();
	}
	
	/**
	 * read the summary data from SUMMARY section in XML and update the capacity_summary table
	 * @param prop
	 */
	private static void processSummary(Property prop) {
		CapacitySummary_Manipulator mani = new CapacitySummary_Manipulator(prop);
		mani.readXML();
		mani.writeDB();
	}
	
	/**
	 * read the data of ticket level from XML
	 * then update the database
	 */
	public static void processTicketLevel(Property prop) {
		Table_Manipulator mani = new TicketLevel_Manipulator(prop);
		mani.readXML();
		mani.writeDB();
	}
	
	/**
	 * read the data of price code information from XML
	 * then update the database
	 */
	public static void processPriceCode(Property prop) {
		Table_Manipulator mani = new PriceCode_Manipulator(prop);
		mani.readXML();
		mani.writeDB();
	}
	
	/**
	 * read the data of seats information from XML
	 * then update the database
	 */
	public static void processSeats(Property prop) {
		Table_Manipulator mani = new Seats_Manipulator(prop);
		mani.readXML();
		mani.writeDB();
	}
	
	/**
	 * Call the capacity controller to get capacity data from xml and add it into database
	 */
	public static void processCapacity(Property prop) {
		Capacity_Controller capController = new Capacity_Controller(prop);
		capController.readXML();
		capController.writeDB();
	}
	
	public void run() {
		OpenHandlers();
		Property prop = new Property();
		processEvent_Opponents(prop);
		processSummary(prop);
		processTicketLevel(prop);
		processPriceCode(prop);
		processSeats(prop);
		processCapacity(prop);
		closeHandlers();
	}
	
	
	public static void main(String args[]) {
		XMLToDB xtd = new XMLToDB("/Users/yg657/Courses/ITP/data/EPL0522E_03.01.xml");
		xtd.run();
	}
	
}
